<template>
    <div class="hotProduct">
        <div class=""  style="position: relative;">
            <div class="hotProductContainerRow">
                <div class="hotProductContainer" :style="{'transform': translate}">
                    <div class="product" v-for="(item,index) in list" :key="index"
                         :style="{'left':(index*item.leftWidth)+'%'}">
                        <div class="productImg">
                            <img :src="item.img" alt="billingham"/>
                        </div>
                    </div>
                </div>
            </div>
            <div class="productNext next" @click="handleNext()">
                <i class="el-icon-arrow-left"></i>
            </div>
            <div class="productNext last" @click="handleLast()">
                <i class="el-icon-arrow-right"></i>
            </div>
        </div>
        <div class="blogBottom">
            <div :class="[currentIndex===index?'blogBottomItem active':'blogBottomItem']" v-for="(item,index) in list" :key="(item+index)">
                <img @click="handleRedirectImg(index)" :src="item.img" alt=""/>
            </div>
        </div>
    </div>
</template>

<script>
    import Vue from 'vue';
    import {Drawer, Popover} from 'element-ui';

    Vue.use(Drawer);
    Vue.use(Popover);
    export default {
        name: 'HotProduct',
        data: () => {
            return {
                translate:'translate(0)',
                leftWidth:100,
                clickCount:0,
                currentIndex:0
            }
        },
        props: {
            msg: String,
            list:Array
        },
        mounted() {
        },
        methods: {
            /**
             * 获取设备宽度
             * @returns {boolean}
             * @private
             */
            _isMobile() {
                const width = document.body.clientWidth;
                if (width <= 992) {
                    return true;
                } else {
                    return false;
                }
            },
            /**
             * 下一个产品
             */
            handleNext(){
                let {clickCount,leftWidth,list,currentIndex}=this;
                clickCount+=1;
                const len=list.length;
                const canMove=Math.floor(100/leftWidth);
                if (currentIndex<canMove-1){
                    this.currentIndex=currentIndex+1;
                    return;
                }
                this.currentIndex=currentIndex+1;
                if (clickCount>(len-canMove)){
                    this.translate='translate(0)';
                    clickCount=0;
                    this.currentIndex=0;
                }else{
                    const leftAll=clickCount*leftWidth;
                    this.translate='translate(-'+leftAll+'%)';
                }
                this.clickCount=clickCount;
            },
            handleLast(){
                let {clickCount,leftWidth,list,currentIndex}=this;
                clickCount-=1;
                const len=list.length;
                const canMove=Math.floor(100/leftWidth);
                if (currentIndex<=canMove-1&&currentIndex>0){
                    this.currentIndex=currentIndex-1;
                    return;
                }
                this.currentIndex=currentIndex-1;
                if (clickCount<0){
                    this.translate='translate(-'+((len-canMove)*leftWidth)+'%)';
                    clickCount=len-canMove;
                    this.currentIndex=len-1;
                }else{
                    const leftAll=clickCount*leftWidth;
                    this.translate='translate(-'+leftAll+'%)';
                }
                this.clickCount=clickCount;
            },
            handleRedirectImg(index){
                const leftWidth=this.leftWidth;
                this.clickCount=index;
                this.currentIndex=index;
                this.translate='translate(-'+(index*leftWidth)+'%)';
            }
        }
    }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
    .hotProduct {
        background: #fff;
    }

    .hotProductTitle {
        font-weight: bold;
        font-size: 18px;
        padding: 20px 0 40px 20px;
    }

    .hotProductContainerRow{
        overflow: hidden;
        margin: 0 auto;
    }

    .hotProductContainer {
        position: relative;
        height: 360px;
        transition: all .3s;
    }

    .hotProductContainer .product {
        width: calc(100% - 10px);
        display: inline-block;
        padding: 0 5px;
        position: absolute;
    }

    .productImg {
        width: 100%;
        display: flex;
        align-items: center;
        -webkit-border-radius: 5px;
        -moz-border-radius: 5px;
        border-radius: 2px;
        justify-items: center;
        text-align: center;
    }

    .productImg img {
        max-width: 360px;
        margin: 0 auto;
        max-height: 360px;
    }

    .productNext {
        position: absolute;
        width: 30px;
        height: 30px;
        line-height: 34px;
        background: #000;
        text-align: center;
        top: 204px;
        cursor: pointer;
        display: none;
    }
    .productNext .el-icon-arrow-left:before{
        color: #d8a030;
        font-size: 20px;
    }
    .productNext .el-icon-arrow-right:before{
        color: #d8a030;
        font-size: 20px;
    }
    .productNext.last{
        right: 0;
    }
    .productNext.next{
        left: 0;
    }

    .blogBottom{
        padding: 20px 0;
        text-align: center;
        display: none;
    }

    .blogBottomItem{
        width: 60px;
        height: 60px;
        background: #fff;
        display: inline-block;
        margin: 0 5px;
        border: 1px solid #fff;
    }
    .blogBottomItem img{
        width: 60px;
        height: 60px;
    }
    .blogBottomItem.active{
        border: 1px solid orange;
    }

    @media screen and (max-width: 420px) {
        .customeRow{
            padding: 0 10px;
            width: calc(100% - 20px);
        }
        .hotProductContainerRow{
            width: 100%;

        }
        .hotProductContainer {
            height: 260px;
            padding: 0 10px;
        }

        .productNext{
            top: 175px;
        }

        .productNext.last{
        }
        .productNext.next{
        }

        .hotProductContainer .product {
            width: 100%;
            padding: 0;
        }

        .productImg {
            height: 240px;
        }

        .productImg img {
            width:240px;
            margin: 0 auto;
        }
    }

    @media screen and (min-width: 420px) and (max-width: 992px) {
        .hotProductContainerRow{
            width: 100%;
        }

        .productNext.last{
            right: 0;
        }
        .productNext.next{
            left: 0;
        }
        .hotProductContainer .product {
            width: 100%;
            padding: 0;
            text-align: center;
        }

        .productImg img {
            width: 50%;
            margin: 0 auto;
        }
    }
</style>
